// src/hooks/useMomentumScroll.ts
import { ref, Ref } from 'vue';

interface MomentumScrollOptions {
  // 减速度系数
  deceleration?: number;
  // 最小速度阈值
  minVelocity?: number;
  // 帧率时间计算方式
  useTimeDelta?: boolean;
}

interface MomentumScrollReturn {
  // 当前速度
  velocity: Ref<number>;
  // 开始拖拽
  startDrag: (startPosition: number, startTime: number) => void;
  // 拖拽中
  onDrag: (currentPosition: number, currentTime: number) => number; // 返回实际移动距离
  // 结束拖拽
  endDrag: () => boolean; // 返回是否有惯性动画
  // 执行惯性动画
  executeMomentum: (
    updatePosition: (displacement: number) => { newPosition: number; limited: boolean },
    onEnd?: () => void
  ) => void;
  // 停止惯性动画
  stopMomentum: () => void;
}

export function useMomentumScroll(options: MomentumScrollOptions = {}): MomentumScrollReturn {
  const {
    deceleration = 0.0015, // 调整默认值
    minVelocity = 0.05,    // 降低阈值
    useTimeDelta = true
  } = options;

  const velocity = ref(0);
  let lastPosition: number | undefined;
  let lastTime: number | undefined;
  let rafId: number | null = null;
  let isDragging = false;

  const startDrag = (startPosition: number, time: number) => {
    // 取消之前的惯性动画
    if (rafId) {
      cancelAnimationFrame(rafId);
      rafId = null;
    }

    lastPosition = startPosition;
    lastTime = time;
    velocity.value = 0;
    isDragging = true;
  };

  const onDrag = (currentPosition: number, currentTime: number): number => {
    if (!isDragging || lastPosition === undefined || lastTime === undefined) {
      return 0;
    }

    const moveDistance = currentPosition - lastPosition;
    const deltaTime = currentTime - lastTime;

    // 使用更平滑的速度计算方式
    if (deltaTime > 0) {
      const currentVelocity = moveDistance / deltaTime;
      // 简单的移动平均来平滑速度
      velocity.value = velocity.value * 0.6 + currentVelocity * 0.4;
    }

    lastPosition = currentPosition;
    lastTime = currentTime;
    
    return moveDistance;
  };

  const endDrag = () => {
    isDragging = false;
    const shouldMomentum = Math.abs(velocity.value) > minVelocity;
    lastPosition = undefined;
    lastTime = undefined;
    return shouldMomentum;
  };

  const executeMomentum = (
    updatePosition: (displacement: number) => { newPosition: number; limited: boolean },
    onEnd?: () => void
  ) => {
    let lastFrameTime: number | null = null;
    
    const animate = (currentTime: number) => {
      if (!rafId) return;

      // 计算时间差，更精确地控制动画
      const deltaTime = lastFrameTime ? currentTime - lastFrameTime : 16;
      lastFrameTime = currentTime;

      // 根据实际时间差计算位移
      const displacement = velocity.value * deltaTime;
      
      // 更新位置并检查是否受限
      const { newPosition, limited } = updatePosition(displacement);

      // 减速 - 使用更自然的减速方式
      const currentDeceleration = deceleration * (1 + Math.abs(velocity.value) * 0.1);
      if (velocity.value > 0) {
        velocity.value = Math.max(0, velocity.value - currentDeceleration * deltaTime);
      } else {
        velocity.value = Math.min(0, velocity.value + currentDeceleration * deltaTime);
      }

      // 如果位置受限，立即停止
      if (limited) {
        velocity.value = 0;
      }

      // 如果速度接近0，停止动画
      if (Math.abs(velocity.value) > 0.001) {
        rafId = requestAnimationFrame(animate);
      } else {
        velocity.value = 0;
        if (onEnd) onEnd();
      }
    };

    // 启动动画
    rafId = requestAnimationFrame((currentTime) => {
      lastFrameTime = currentTime;
      animate(currentTime);
    });
  };

  const stopMomentum = () => {
    if (rafId) {
      cancelAnimationFrame(rafId);
      rafId = null;
    }
    velocity.value = 0;
  };

  return {
    velocity,
    startDrag,
    onDrag,
    endDrag,
    executeMomentum,
    stopMomentum
  };
}